package com.rocsun.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rocsun.base.RocsunErrorCodeEnum;
import com.rocsun.base.RocsunException;
import com.rocsun.entity.UserDO;
import com.rocsun.repository.UserRepository;
import com.rocsun.service.UserService;
import com.rocsun.service.convertor.UserConvertor;
import com.rocsun.user.param.UserRequest;
import com.rocsun.user.result.UserVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * @program: rocsun-scali
 * @description:
 * @author: yx.guo
 * @create: 2025-10-19 14:51
 **/
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserRepository userRepository;

    @Override
    public IPage<UserVO> page(UserRequest request) {
        Page<UserDO> page = new Page<>(request.getCurrent(), request.getPageSize());
        LambdaQueryWrapper<UserDO> queryWrapper = new LambdaQueryWrapper<UserDO>();
        queryWrapper.like(StrUtil.isNotBlank(request.getUsername()), UserDO::getUsername, request.getUsername());
        queryWrapper.eq(ObjectUtil.isNotEmpty(request.getStatus()), UserDO::getStatus, request.getStatus());
        queryWrapper.eq(StrUtil.isNotBlank(request.getRole()), UserDO::getRole, request.getRole());
        queryWrapper.orderByDesc(UserDO::getId);

        return userRepository.page(page, queryWrapper).convert(UserConvertor::convert);
    }

    @Override
    public UserVO getByUserName(String userName) {
        UserDO userDO = userRepository.lambdaQuery().eq(UserDO::getUsername, userName).one();
        if(Objects.isNull(userDO)) {
            return null;
        }
        return UserConvertor.convert(userDO);
    }

    @Override
    public UserVO getById(Long id) {
        UserDO userDO = userRepository.lambdaQuery().eq(UserDO::getId, id).one();
        if(Objects.isNull(userDO)) {
            return null;
        }
        return UserConvertor.convert(userDO);
    }

    @Override
    public boolean physicalDelete(Long id) {
        UserDO userDO = userRepository.getBaseMapper().selectById(id);
        if (Objects.isNull(userDO)) {
            throw new RocsunException(RocsunErrorCodeEnum.PARAM_ERROR.getCode(), "未查询到用户：" + id);
        }

        return userRepository.physicalDelete(id);
    }

    @Override
    public boolean logicDelete(Long id) {
        UserDO userDO = userRepository.getBaseMapper().selectById(id);
        if (Objects.isNull(userDO)) {
            throw new RocsunException(RocsunErrorCodeEnum.PARAM_ERROR.getCode(), "未查询到用户：" + id);
        }
        return userRepository.removeById(id);
    }

    @Override
    public boolean update(UserRequest userRequest) {
        UserDO userDO = UserConvertor.convert(userRequest);
        return userRepository.updateById(userDO);
    }

    @Override
    public int create(UserRequest request) {
        UserDO userDO = userRepository.lambdaQuery().eq(UserDO::getUsername, request.getUsername()).one();
        if (Objects.nonNull(userDO)) {
            throw new RocsunException(RocsunErrorCodeEnum.PARAM_ERROR.getCode(), "该用户已经存在：" + request.getUsername());
        }

        return userRepository.getBaseMapper().insert(UserConvertor.convert(request));
    }

    @Override
    public UserVO login(UserRequest request) {
        UserDO userDO = userRepository.lambdaQuery().eq(UserDO::getUsername, request.getUsername()).one();
        if (Objects.isNull(userDO)) {
            return null;
        }

        if (StrUtil.equals(userDO.getPassword(), request.getPassword())) {
            userDO.setLastLoginAt(LocalDateTime.now());
            userRepository.updateById(userDO);
            return UserConvertor.convert(userDO);
        }

        return null;
    }

}
